﻿namespace _05.BitArray64Exercise
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;

    public class BitArray64 : IEnumerable<int>
    {
        public BitArray64(ulong number)
        {
            this.Number = number;
        }

        public ulong Number { get; set; }

        public int this[int index]
        {
            get
            {
                if (index < 0 || index > 63)
                {
                    throw new IndexOutOfRangeException("Index must be in range from 0 to 63!");
                }

                return (int)((this.Number >> index) & 1);
            }
        }

        public static bool operator ==(BitArray64 firstBitArray, BitArray64 secondBitArray)
        {
            return BitArray64.Equals(firstBitArray, secondBitArray);
        }

        public static bool operator !=(BitArray64 firstBitArray, BitArray64 secondBitArray)
        {
            return !BitArray64.Equals(firstBitArray, secondBitArray);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public IEnumerator<int> GetEnumerator()
        {
            for (int i = 63; i >= 0; i--)
            {
                yield return this[i];
            }
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = 17;
                result = (result * 23) + this.Number.GetHashCode();
                return result;
            }
        }

        public bool Equals(BitArray64 bitArrayNumber)
        {
            if (ReferenceEquals(null, bitArrayNumber))
            {
                return false;
            }

            if (ReferenceEquals(this, bitArrayNumber))
            {
                return true;
            }

            return this.Number == bitArrayNumber.Number;
        }

        public override bool Equals(object obj)
        {
            BitArray64 bitArrayNumber = obj as BitArray64;
            if (bitArrayNumber == null)
            {
                return false;
            }

            return this.Equals(bitArrayNumber);
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder(64);
            for (int i = 63; i >= 0; i--)
            {
                sb.Append(this[i]);
            }

            return sb.ToString();
        }
    }
}